package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorLeaseSpaceNew;
import guoanmaker.operator.business.model.ProvideRoom;
import guoanmaker.operator.business.modelnobind.ProvideRoomMove;
import guoanmaker.operator.business.repository.OperatorLeaseSpaceNewRepository;
import guoanmaker.operator.business.repository.OperatorRoomRepository;

@Service
public class OperatorLeaseSpaceNewService implements OperatorLeaseSpaceNewServiceInterface{
	@Autowired
	private OperatorLeaseSpaceNewRepository operatorLeaseSpaceNewRepository;
	@Autowired
	private OperatorRoomRepository operatorRoomRepository;
	@PersistenceContext
	private EntityManager manager;
	/**
	 * 
	    * @Title: saveOperatorLeaseSpaceNew  
	    * @Description: 保存新版租赁空间  
	    * @param @param inRoundPhone
	    * @throws
	 */
	@Override
	@Transactional
	public void saveOperatorLeaseSpaceNew(InRoundPhone inRoundPhone) {
		/*List<OperatorLeaseSpaceNew> allOperatorLeaseSpaceNew = operatorLeaseSpaceNewRepository.findOperatorLeaseSpaceNewByCityCode(inRoundPhone.getCityId());
		if (allOperatorLeaseSpaceNew != null && allOperatorLeaseSpaceNew.size() >0) {
			
		}*/
		 OperatorLeaseSpaceNew[] operatorLeaseSpaceNews= inRoundPhone.getOperatorLeaseSpaceNew();
		 for (OperatorLeaseSpaceNew operatorLeaseSpaceNew : operatorLeaseSpaceNews) {
			 operatorLeaseSpaceNew.setCityCode(inRoundPhone.getCityId());
			 operatorLeaseSpaceNewRepository.save(operatorLeaseSpaceNew);
		}
		 
		
	}

	/**
	 * 
	    * @Title: selectProvideRoom  
	    * @Description: 查询空间  
	    * @param @param spaceNumber
	    * @return ProvideRoomMove      
	 */
	@Override
	public ProvideRoomMove selectProvideRoom(String spaceNumber) {
		String jpql = "select new guoanmaker.operator.business.modelnobind.ProvideRoomMove(p.id,p.describeAsk,p.roomName,p.roomCodes,"
				+ "c.name,a.name,e.name,p.address,p.complement,p.roomlogo,p.spaceDetail,p.roomImgOne,"
				+ "p.roomImgTwo,p.roomImgThree,p.roomImgFour,p.roomImgFive,p.roomTime) from ProvideRoom p left join p.city c left join p.area a left join p.province e where p.roomState=2 and p.roomCodes=:spaceNumber";
		Query query = manager.createQuery(jpql);
		query.setParameter("spaceNumber", spaceNumber);
		ProvideRoomMove provideRoomMove = null;
		if (query.getResultList() != null && query.getResultList().size() >0) {
			provideRoomMove = (ProvideRoomMove)query.getSingleResult();
		}
		return provideRoomMove;
	}
	
	/**
	 * 
	    * @Title: findOperatorLeaseSpaceNewByCityCode  
	    * @Description:根据citycode 查询配置的租赁空间   
	    * @param @param cityCode
	    * @return List<OperatorLeaseSpaceNew>
	    * @throws
	 */
	@Override
	public List<OperatorLeaseSpaceNew> findOperatorLeaseSpaceNewByCityCode(String cityCode) {
		return operatorLeaseSpaceNewRepository.findOperatorLeaseSpaceNewByCityCode(cityCode);
	}

	@Override
	@Transactional
	public void deleteByCityCode(InRoundPhone inRoundPhone) {
		operatorLeaseSpaceNewRepository.deleteOperatorLeaseSpaceNewByCityCode(inRoundPhone.getCityId());
		
	}



}
